﻿namespace Sidvall.ExceptionHandling
{
    public class UserExceptionProvider : IExceptionProvider
    {
        #region Public Members

        #region GetMessage

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        public virtual string GetMessage(System.Collections.Generic.IEnumerable<System.Exception> exceptions, string messageSeparator, string sectionSeparator)
        {
            string message;

            var sb = new System.Text.StringBuilder();
            if (exceptions != null)
            {
                foreach (var exception in exceptions)
                {
                    if (sb.Length > 0)
                        sb.Append(messageSeparator);
                    var baseException = exception.GetBaseException();
                    if (baseException is Sidvall.Security.UnauthenticatedException)
                    {
                        var e = baseException as Sidvall.Security.UnauthenticatedException;
                        message = e.GetMessageResolved();
                    }
                    else if (baseException is Sidvall.Security.UnauthorizedException)
                    {
                        var e = baseException as Sidvall.Security.UnauthorizedException;
                        message = e.GetMessageResolved();
                    }
                    else if (baseException is Sidvall.Business.BusinessException)
                    {
                        var e = baseException as Sidvall.Business.BusinessException;
                        message = e.GetMessageResolved();
                    }
                    else if (baseException is Sidvall.Data.ConstraintException)
                    {
                        var e = baseException as Sidvall.Data.ConstraintException;
                        message = e.GetMessageResolved();
                    }
                    else if (baseException is Sidvall.Data.UpdateConcurrencyException)
                    {
                        var e = baseException as Sidvall.Data.UpdateConcurrencyException;
                        message = e.GetMessageResolved();
                    }
                    else if (baseException is Sidvall.Net.HttpException)
                    {
                        var e = baseException as Sidvall.Net.HttpException;
                        message = e.GetMessageResolved();
                    }
                    else
                    {
                        message = baseException.Message;
                    }
                    sb.Append(message);
                }
            }
            return sb.ToString();
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "ExceptionProvider - User";
        }

        #endregion

        #endregion
    }
}
